package com.qa.listener;

import com.qa.constant.RabbitMqConstant;
import com.qa.service.QuestionCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SpringRabbitListener {

    @Autowired
    private QuestionCategoryService questionCategoryService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstant.QUEUE_ADD_QUESTION),
            exchange = @Exchange(name = RabbitMqConstant.DIRECT_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {RabbitMqConstant.KEY_ADD_QUESTION}
    ))
    public void listenDirectQueueAddQuestion(String msg) {
        log.info("消费者接收到qa.add_question_queue的消息：{}", msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstant.QUEUE_DELETE_QUESTION_CATEGORY),
            exchange = @Exchange(name = RabbitMqConstant.DIRECT_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = {RabbitMqConstant.KEY_DELETE_QUESTION_CATEGORY}
    ))
    public void listenDirectQueueDeleteQuestionCategory(String msg) {
        log.info("消费者接收到qa.delete_question_category的消息：{}", msg);
        long categoryId = Long.parseLong(msg);
        // 删除该分类下的所有题目
        questionCategoryService.deleteQuestionsInCategory(categoryId);
    }
}